
import 'package:eshi/app/MyApp.dart';
import 'package:eshi/app/settings/PrivacyPolicyPage.dart';
import 'package:eshi/app/shi/data/Config.dart';
import 'package:eshi/app/shi/data/System.dart';
import 'package:eshi/app/shi/data/tabtxt_history.dart';
import 'package:eshi/app/shi/widget/CommonWidgets.dart';
import 'package:flutter/material.dart';
import 'package:eshi/main.dart';
import 'package:eshi/app/shi/data/History.dart';
import 'package:eshi/app/shi/taxis.dart';
import 'package:html/parser.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({super.key, required this.title,this.path});

  final String title;
  List<String>? path;

  @override
  State<MyHomePage> createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  History? nowHistory;
  var isButtonShow =true;

    @override
  void initState(){
    super.initState();

    _checkPrivacyStatus();
    //配置需要加载的history
    if(widget.path!=null && widget.path!.isNotEmpty){
      TabTxtHistory.load(widget.path![0]).then((mHistory){
         mHistory.defaultPath = widget.path!.sublist(1).join("/");
         changeHistory(mHistory );
      });
      widget.path = null;
      return;
    }
    TabTxtHistory.load(Config.lastHistory).then((mHistory) => changeHistory(mHistory) );
  }



   // 检查是否需要显示隐私政策弹窗
  Future<void> _checkPrivacyStatus() async {
    final prefs = await SharedPreferences.getInstance();
    final hasShown = prefs.getBool('privacy_shown') ?? false;
    // 首次启动，显示弹窗
    if (!hasShown && mounted && System.isPhone) {
        showDialog(
          context: context,
          barrierDismissible: false, // 不允许点击外部关闭
          builder: (context) => PrivacyPolicyDialog(
            onResult: (agreed) {
              if (agreed) {
                prefs.setBool('privacy_shown',true);
                // 用户同意，继续正常流程
              } else {
                // 用户拒绝，可退出应用或限制功能
                // 注意：iOS可能不允许因拒绝隐私政策而强制退出
              }
            },
          ),
        );
    }
  }
  @override
  Widget build(BuildContext context) {
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // NumInputWidget(),
            if(nowHistory!=null)
                TimeAxisWidget(nowHistory!,key: customWidgetKey)
          ],
        ),
      ),
     
      floatingActionButton: Visibility(
        visible: isButtonShow,
        child:GestureDetector(
        //长按菜单 
         onLongPressStart: (details) =>   simpleMenu(context:context, position: details.globalPosition,
            items: ['时间轴',"配置页","内容编辑",'搜索',"显示模式","背景颜色"],
            onChange: (s) =>  customWidgetKey.currentState?.tabState?.pageSetting(s)
          ),
         //双击配置 
         onDoubleTap: () =>  Navigator.pushNamed(context, '/settings'),
         //单击时间轴 
         onTap: () => customWidgetKey.currentState?.onTimeLineButtonClick(),
         child: FloatingActionButton(
          onPressed:null,
          // tooltip: mt('时间轴'),
            // 自定义形状 - 使用更精确的圆形边界
          shape: const CircleBorder(
            side: BorderSide.none, // 无边框
          ),
          child: const Icon(Icons.history),
        )),
        ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void changeHistory(History mHistory,[String path=""]) {
    setState(() {
      print("CurrentHistory:${mHistory.name}");
      nowHistory = mHistory;
    });
  } 
   void setButtonShow(bool isShow) {
    setState(() {
      isButtonShow = isShow;
    });
  }
}
